package cn.cvs.controller;

import cn.cvs.entity.Auctionuser;
import cn.cvs.service.AuctionuserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *用户控制器
 */
@Controller
public class AuctionuserController {
    @Resource
    private AuctionuserService auctionuserService;

    @RequestMapping("/Sregister")
    public String addUser(Auctionuser user,String inputCode,HttpSession session,Model model){
        String yzm = (String) session.getAttribute("numrand");
        System.out.println(yzm);
        System.out.println(inputCode);
        System.out.println(user.toString());
        if(yzm.equals(inputCode)){
            if(auctionuserService.addUser(user)>0){
                return "login";
            }else
                model.addAttribute("error","用户注册未成功！！！");
        }else
            model.addAttribute("error","验证码不正确！！！");
        return "register";
    }

    @RequestMapping("/enroll")
    public String toEnroll(){
        return "register";
    }

    @RequestMapping("/login")    //登录
    public String login(String username, String userpwd, String inputCode, HttpSession session, Model model){
        String yzm = (String) session.getAttribute("numrand");
        if(yzm.equals(inputCode)){
            Auctionuser user = auctionuserService.login(username,userpwd);
            if(user!=null) {
                session.setAttribute("auctionUserSession", user);
                return "redirect:/SrecordList";
            }else
                model.addAttribute("error","账号或密码错误！！！");
        }else
            model.addAttribute("error","验证码不正确！！！");
        return "login";

    }

}
